package com.dlb.andorid.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.dlb.andorid.R;
import com.dlb.andorid.XContract;
import com.dlb.andorid.bean.Coupon;
import com.dlb.andorid.presenter.CouponPresenter;
import com.kwan.xframe.mvp.presenter.BasePresenter;
import com.kwan.xframe.mvp.view.activity.BaseCommonActivity;

import java.util.ArrayList;
import java.util.List;

public class CouponListActivity extends BaseCommonActivity implements XContract.ICouponView {

	private CouponPresenter mPresenter;

	private boolean isNeedResult;

	@Override
	protected void beForeSetContentView() {
		super.beForeSetContentView();
		if (getIntentData("isNeedResult") != null)
			isNeedResult = (boolean) getIntentData("isNeedResult");
	}

	@Override
	protected BasePresenter getBasePresenter() {
		mPresenter = new CouponPresenter(this);
		return mPresenter;
	}

	private RecyclerView mRecyclerView;
	private TextView tv_state1, tv_state2, tv_state3, tv_state_last;
	private View ll_state1, ll_state2, ll_state3, v_state1, v_state2, v_state3, v_state_last;


	private List<Coupon> mAll = new ArrayList<>();

	private List<Coupon> mData1 = new ArrayList<>();
	private List<Coupon> mData2 = new ArrayList<>();
	private List<Coupon> mData3 = new ArrayList<>();

	BaseQuickAdapter<Coupon, BaseViewHolder> mAdapter;

	@Override
	protected void initViews() {
		super.initViews();

		tv_state1 = findViewById(R.id.tv_state1);
		tv_state2 = findViewById(R.id.tv_state2);
		tv_state3 = findViewById(R.id.tv_state3);
		ll_state1 = findViewById(R.id.ll_state1);
		ll_state2 = findViewById(R.id.ll_state2);
		ll_state3 = findViewById(R.id.ll_state3);
		v_state1 = findViewById(R.id.v_state1);
		v_state2 = findViewById(R.id.v_state2);
		v_state3 = findViewById(R.id.v_state3);

		tv_state_last = tv_state1;
		v_state_last = v_state1;

		ll_state1.setOnClickListener(this);
		ll_state2.setOnClickListener(this);
		ll_state3.setOnClickListener(this);

		mRecyclerView = findViewById(R.id.recycleView);
		mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
		mRecyclerView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
		mRecyclerView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
		mRecyclerView.setLayerType(View.LAYER_TYPE_NONE, null);

		mRecyclerView.setHasFixedSize(true);
		mRecyclerView.setItemAnimator(null);
		mRecyclerView.setNestedScrollingEnabled(false);


		mAdapter = new BaseQuickAdapter<Coupon, BaseViewHolder>(R.layout.list_item_coupon, mData1) {
			@Override
			protected void convert(BaseViewHolder helper, Coupon item) {
				helper.setText(R.id.tv_money, item.getMoney_num() + "");
				helper.setText(R.id.tv_remark, item.getRemarks());
				helper.setText(R.id.tv_name, item.getCoupon_name());
				helper.setText(R.id.tv_time, item.getOvertime());
			}
		};

		mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
			@Override
			public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
				if (isNeedResult && mCurrentIndex == 1) {
					Intent intent = new Intent();
					Bundle bundle = new Bundle();
					bundle.putSerializable("coupon", mData1.get(position));
					intent.putExtra("data", bundle);
					setResult(100, intent);
					finish();
				}
			}
		});

		mRecyclerView.setAdapter(mAdapter);
	}

	@Override
	protected void initData() {
		mPresenter.getCouponList();
	}

	@Override
	public void onClick(View v) {
		super.onClick(v);
		if (v == ll_state1 && tv_state_last != tv_state1) {
			setCheck(1);
		} else if (v == ll_state2 && tv_state_last != tv_state2) {
			setCheck(2);
		} else if (v == ll_state3 && tv_state_last != tv_state3) {
			setCheck(3);
		}
	}

	int mCurrentIndex = 1;

	public void setCheck(int index) {
		mCurrentIndex = index;
		tv_state_last.setTextColor(Color.BLACK);
		v_state_last.setVisibility(View.INVISIBLE);

		switch (index) {
			case 1:
				tv_state_last = tv_state1;
				v_state_last = v_state1;

				tv_state1.setTextColor(getResources().getColor(R.color.colorPrimary));
				v_state1.setVisibility(View.VISIBLE);
				mAdapter.setNewData(mData1);

				break;
			case 2:
				tv_state_last = tv_state2;
				v_state_last = v_state2;

				tv_state2.setTextColor(getResources().getColor(R.color.colorPrimary));
				v_state2.setVisibility(View.VISIBLE);
				mAdapter.setNewData(mData2);

				break;
			case 3:
				tv_state_last = tv_state3;
				v_state_last = v_state3;

				tv_state3.setTextColor(getResources().getColor(R.color.colorPrimary));
				v_state3.setVisibility(View.VISIBLE);
				mAdapter.setNewData(mData3);

				break;
		}
	}


	@Override
	protected int getTopViewId() {
		return 0;
	}

	@Override
	protected int getMainViewId() {
		return R.layout.activity_coupon_list;
	}

	@Override
	protected int getBottomViewId() {
		return 0;
	}

	@Override
	protected String getTitleTxt() {
		return "优惠券";
	}

	@Override
	public String getPageName() {
		return null;
	}

	@Override
	public void getCouponListSuccess(List<Coupon> data) {

		mAll.clear();
		mAll.addAll(data);

		mData1.clear();
		mData2.clear();
		mData3.clear();

		for (Coupon coupon : mAll) {
			if (coupon.getStatus() == 0) {
				mData1.add(coupon);
			} else if (coupon.getStatus() == 1) {
				mData2.add(coupon);
			} else if (coupon.getStatus() == 2) {
				mData3.add(coupon);
			}
		}

		Log.e("kwan", "size:" + mData1.size());
		mAdapter.setNewData(mData1);

	}
}
